package daoImpls;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

import daoFactory.DaoFactory;
import vo.Category;
import vo.PetObject;

public class CategoryDAOImpl<E> extends ObjectDAOImpl {
	
    @Override
    public Category get(String id) {
        Category category = null;
        try(Connection c = cp.getConnection(); Statement s = c.createStatement()) {
            ResultSet resultSet =s.executeQuery("select * from category where catID=" + id);
            if (resultSet.next()){
                category = new Category();
                category.setCatID(id);
                category.setCatName(resultSet.getString(2));
            }
            cp.returnConnection(c);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return category;
    }

    @Override
    public void add(PetObject hero) {
        Category category =(Category) hero;
        try(Connection c = cp.getConnection(); PreparedStatement s =
                c.prepareStatement("insert into category values(null,?)")) {
        s.setString(1,category.getCatName());
        s.execute();
        cp.returnConnection(c);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void update(PetObject hero) {
        Category category =(Category) hero;
        try(Connection c = cp.getConnection(); PreparedStatement s =
                c.prepareStatement("update category set CatName = ? where CatID=" + category.getCatID())) {
            s.setString(1,category.getCatName());
            s.execute();
            cp.returnConnection(c);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    public void delete(String id) {
        super.delete(id,"CatID");
    }
    public void deleteQuery(String ids){
    	super.deleteQuery(ids, "CateID");
    }
    
    public List<PetObject> list(){
 	   List<PetObject> list = new ArrayList<>();
       try(Connection c = cp.getConnection();){
       	PreparedStatement p = c.prepareStatement("SELECT * FROM category order by CatID ");
       	ResultSet rs = p.executeQuery();
       	while(rs.next()){
       		Category cate = new Category();
       		cate.setCatID(rs.getString(1));
       		cate.setCatName(rs.getString(2));
       		list.add(cate);
       	}
       	p.close();
    	cp.returnConnection(c);
       } catch (SQLException e) {
			e.printStackTrace();
		}
       
       return list;
    }
    public List<PetObject> list(int start, int count) {
        List<PetObject> list = new ArrayList<>();
        try(Connection c = cp.getConnection();){
        	PreparedStatement p = c.prepareStatement("SELECT * FROM 'category' order by CatID limit ?,?");
        	p.setInt(1, start);
        	p.setInt(2, count);
        	ResultSet rs = p.executeQuery();
        	while(rs.next()){
        		Category cate = new Category();
        		cate.setCatID(rs.getString(1));
        		cate.setCatName(rs.getString(2));
        		list.add(cate);
        	}
        	p.close();
        	cp.returnConnection(c);
        } catch (SQLException e) {
			e.printStackTrace();
		}
        
        return list;
    }
    
}
